package quq.missq.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.beans.IdeaFeedBack;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;
/**
 * Created by wcx on 2015/9/21.
 */
public class IdeaFeedBackActivity extends BaseActivity{//意见反馈

    private TextView tv_confirm;
    private EditText et_editor;
    private TextView tv_charnumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ideafeedback);
        initView();
        initListener();
    }


    private int cou = 0;
    int selectionEnd = 0;
    private int mMaxLenth=100;
    public  String stringFilter(String str)throws PatternSyntaxException {
        String regEx = "[/\\:*?<>|\"\n\t]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("");
    }

    private void initListener() {
        et_editor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                cou = before + count;
                String editable = et_editor.getText().toString();
                String str = stringFilter(editable); //过滤特殊字符
                if (!editable.equals(str)) {
                    et_editor.setText(str);
                }
                et_editor.setSelection(et_editor.length());
                cou = et_editor.length();
                tv_charnumber.setText(s.length() + "");
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (cou > mMaxLenth) {
                    selectionEnd = et_editor.getSelectionEnd();
                    s.delete(mMaxLenth, selectionEnd);
                }
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendIdea();
            }
        });
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //发送意见反馈
    private void sendIdea() {
        String idea= et_editor.getText().toString();
        String acc_token=UserTools.getUser(this).getAcc_token();
        if(idea.equals("")){
            Toast.makeText(this,"必须输入内容.....",Toast.LENGTH_LONG).show();
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("content",idea);
        map.put("acc_token", acc_token);

        VolleyTool.post(this, Constants.SEND_IDEA, map, new VolleyTool.HTTPListener() {
            @Override
            public <T> void onResponse(T t, int requestCode) {
                IdeaFeedBack info= (IdeaFeedBack) t;
                if(info.getCode()==0){
                    showToast(info.getMessage());
                    finish();
                }else{
                    showToast(info.getMessage());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error, int requestCode) {

            }
        },0, IdeaFeedBack.class);
    }

    private void initView() {
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        et_editor = (EditText) findViewById(R.id.et_editor);
        tv_charnumber = (TextView) findViewById(R.id.tv_charnumber);
    }
}
